<?php
class usersQuery extends EngineSupervisor
{
        private $db;

        public function __construct()
        {
                $this-> db = new dataBase();
        }
        public function selectLastWatch($userId)
        {
//            pre($userId);
            $sQuery = "SELECT s.id as serial_id, s.note, e.episode, e.season, s.polish_name, s.name FROM last_watch lw, shows e, serials s
                        WHERE s.id=e.serial_id AND e.id=lw.episode_id AND user_id=? ORDER BY lw.creation_date DESC LIMIT 10";
            $aResult = $this -> db -> ExecuteQuery($sQuery, array($userId));

            return $aResult;
        }

        public function getFavouritesSerials($userId)
        {
            $sQuery = "SELECT s.* FROM serials s, favourites_serials fs WHERE fs.user_id=? AND s.id=fs.serial_id ORDER BY s.name";
            $aResult = $this -> db -> ExecuteQuery($sQuery, array($userId));

            return $aResult;
        }

        public function getTheSameFavouritesSerials($serialId, $userId)
        {
            $sQuery = "SELECT u.* FROM users u, favourites_serials fs WHERE fs.serial_id=? AND fs.user_id <> ? AND u.id=fs.user_id";
            $aResult = $this -> db -> ExecuteQuery($sQuery, array($serialId, $userId));

            return $aResult;
        }

        public function getSerialNews($serialId)
        {
            $sQuery = "SELECT * FROM news WHERE serial_id=?";
            $aResult = $this -> db -> ExecuteQuery($sQuery, array($serialId));

            return $aResult;
        }

        public function getFavouritesUserSerials($userId)
        {
//            pre($userId);
            $sQuery = "SELECT s.name, s.id FROM serials s, favourites_serials fs WHERE s.id=fs.serial_id AND fs.user_id=?";
            $aResult = $this -> db -> ExecuteQuery($sQuery, array($userId));
//            $sQuery = "SELECT * FROM favourites_serials WHERE user_id=?";
//            $aResult = $this -> db -> ExecuteQuery($sQuery, array($userId));

            return $aResult;
        }

        public function deleteSerialFromFav($serialId, $userId)
        {
            $sQuery = "DELETE FROM favourites_serials WHERE serial_id=? AND user_id=?";
            $aResult = $this -> db -> ModifyQuery($sQuery, array($serialId, $userId));

            return $aResult;
        }

}
?>